﻿Imports Microsoft.Office.Tools.Ribbon
Imports System.Environment
Imports System.IO
Imports System.Diagnostics

Public Class mainRibbon
    Private Sub mainRibbon_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load
        'Put favorites into menu for favorites
        initializeFavorites()

        'Put search engine options into dropdown
        initializeSearchEngines()
    End Sub

#Region "Favorites Methods"
    Private Sub initializeFavorites()
        'Get favorites folder
        Dim path As String = GetFolderPath(SpecialFolder.Favorites)
        Dim favFolder As New DirectoryInfo(path)

        For Each di As DirectoryInfo In favFolder.GetDirectories
            AddDirectoryToMenu(di, Me.menuIEFavorites)
        Next
        AddUrlsToMenu(favFolder, Me.menuIEFavorites)
    End Sub

    Private Sub AddDirectoryToMenu(ByVal dirInfo As DirectoryInfo, ByRef ParentMenu As RibbonMenu)
        Dim dirMenu As New RibbonMenu
        dirMenu.Label = dirInfo.Name
        dirMenu.Dynamic = True
        ParentMenu.Items.Add(dirMenu)
        For Each di As DirectoryInfo In dirInfo.GetDirectories
            AddDirectoryToMenu(di, dirMenu)
        Next
        AddUrlsToMenu(dirInfo, dirMenu)
    End Sub

    Private Sub AddUrlsToMenu(ByVal dirInfo As DirectoryInfo, ByRef ParentMenu As RibbonMenu)
        For Each fi As FileInfo In dirInfo.GetFiles
            If Path.GetExtension(fi.FullName).Equals(".url") Then
                Dim btnUrl As New RibbonButton()
                btnUrl.Label = Path.GetFileNameWithoutExtension(fi.FullName)
                btnUrl.Tag = fi.FullName
                AddHandler btnUrl.Click, AddressOf IEFavoritesClickHandler
                ParentMenu.Items.Add(btnUrl)
            End If
        Next
    End Sub

    Private Sub IEFavoritesClickHandler(ByVal sender As Object, ByVal e As RibbonControlEventArgs)
        Dim btnClicked As RibbonButton = CType(sender, RibbonButton)
        Dim filePath As String = btnClicked.Tag
        System.Diagnostics.Process.Start(filePath)
    End Sub
#End Region

#Region "Search Methods"
    Private Sub initializeSearchEngines()
        Dim engines As New IESearchEngines
        For Each engine As IESearchEngines.SearchEngine In engines
            Dim item As New RibbonDropDownItem
            item.Label = engine.DisplayName
            item.Tag = engine.SearchString
            cmbSearchEngines.Items.Add(item)
        Next

        'set default
        cmbSearchEngines.Text = engines.DefaultEngine.DisplayName
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles btnSearch.Click
        Process.Start(GetSearchURL(txtSearch.Text))
    End Sub

    Private Function ConvertStringToURI(ByRef inputString As String) As String
        Dim URIOutput As New StringBuilder
        For Each c As Char In inputString
            If Char.IsLetterOrDigit(c) Then
                'pass character through to output
                URIOutput.Append(c)
            ElseIf c = Char.Parse(" ") Then
                URIOutput.Append("+")
            Else
                URIOutput.Append(Uri.HexEscape(c))
            End If

        Next
        Return (URIOutput.ToString)
    End Function

    Private Function GetSearchURL(ByVal searchText As String) As String
        Dim searchURL As String = ""
        For Each item As RibbonDropDownItem In cmbSearchEngines.Items
            If cmbSearchEngines.Text = item.Label Then
                searchURL = item.Tag
            End If
        Next

        searchURL = searchURL.Replace("{searchTerms}", ConvertStringToURI(searchText))

        Return searchURL
    End Function
#End Region
End Class
